﻿package com.red.battleworm.controller {
	import com.red.battleworm.AppMain;	
	import com.red.battleworm.protocols.*;	
	import com.red.net.SocketEvent;	
	import com.red.battleworm.model.*;	
	import com.red.battleworm.view.*;	
	
	import org.puremvc.as3.patterns.command.*;
	import org.puremvc.as3.interfaces.*;	

	/**
	 * @author Administrator
	 */
	public class LoginCmd extends SimpleCommand {
		
		private var loginMediator : LoginMediator;
		
		/**
		 * 发送用户名和密码到服务端，并设定服务端返回时执行ValidateCmd
		 */
		override public function execute( note:INotification ) : void{
			loginMediator = LoginMediator(facade.retrieveMediator("LoginMediator"));
			var conn : NetProxy = NetProxy(facade.retrieveProxy("NetProxy"));
			var args:Array = note.getBody() as Array;
			var username:String = args["username"];
			var password:String = args["password"];
			var protocol:Protocol = ProtocolFactory.getLoginProtocol(username, password);
			conn.sendAndListen(protocol.format(), onReceive);
		}
		
		private function onReceive(event : SocketEvent) : void {
			var xml : XML = XML(event.data);
			trace(xml.@type);
			if(xml.@type == '0x01'){
				loginMediator.destroy();
				sendNotification(AppMain.SHOWROOM, event.data);
			}
		}
	}
}
